//
//  DiscoveryViewController.m
//  halfSweat
//
//  Created by 甘伟杰 on 16/5/13.
//  Copyright © 2016年 ganweijie. All rights reserved.
//  http://img4q.duitang.com/uploads/people/201605/12/20160512144133_TUt5W.thumb.640_160_c.jpeg
/*
 
 
 
 */
#import "DiscoveryViewController.h"
#import "SectionOneCell.h"
#import "SectionTwoCell.h"
#import "SectionThreeCell.h"
#import "SectionForthCell.h"
#import "DiscoverySeparatorCell.h"

#import "SectionItem.h"
#import "RowItem.h"

#import <UIImageView+WebCache.h>

static NSString * const kSectionOneCell = @"SectionOne";
static NSString * const kSectionTwoCell = @"SectionTwo";
static NSString * const kSectionThreeCell = @"SectionThree";
static NSString * const kSectionForthCell = @"SectionForth";
static NSString * const kSectionSeparatorCell = @"SeparatorSection";

@interface DiscoveryViewController ()

/** 是否有主题 */
@property (assign, nonatomic, getter=isPicture) BOOL picture;

/** 组模型数组 */
@property (strong, nonatomic) NSMutableArray *sectionGroups;


@end

@implementation DiscoveryViewController
#pragma mark - 懒加载项
- (NSMutableArray *)sectionGroups {
    if (_sectionGroups == nil) {
        _sectionGroups = [NSMutableArray array];
    }
    return _sectionGroups;
}

-(instancetype)init {   // 初始化为group样式
    if (self = [super initWithStyle:UITableViewStyleGrouped]) {
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    NSDictionary *dictionary = @{
                                 @"美食菜谱" : @"http://img4q.duitang.com/uploads/people/201602/14/20160214154703_scf2F.thumb.72_72_c.jpeg",
                                 @"收工DIY" : @"http://img4q.duitang.com/uploads/people/201602/14/20160214173638_LKuBH.thumb.72_72_c.jpeg",
                                 @"插画绘画" : @"http://img4q.duitang.com/uploads/people/201602/14/20160214170314_2kRKG.thumb.72_72_c.jpeg",
                                 @"家居生活" : @"http://img4q.duitang.com/uploads/people/201602/02/20160202112337_mPY8J.thumb.72_72_c.jpeg",
                                 @"旅行" : @"http://img4q.duitang.com/uploads/people/201602/14/20160214171825_WFJYM.thumb.72_72_c.jpeg",
                                };
    // 设置tableView属性
    self.tableView.contentInset = UIEdgeInsetsMake(-35, 0, 0, 0);
    self.tableView.sectionHeaderHeight = 0;
    self.tableView.sectionFooterHeight = 0;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    // 注册Cell 的类型
    UINib *nib = [UINib nibWithNibName:NSStringFromClass([SectionOneCell class]) bundle:nil];
    [self.tableView registerNib:nib forCellReuseIdentifier:kSectionOneCell];
    
    [self.tableView registerClass:[SectionTwoCell class] forCellReuseIdentifier:kSectionTwoCell];
    
    UINib *nibThree = [UINib nibWithNibName:NSStringFromClass([SectionThreeCell class]) bundle:nil];
    [self.tableView registerNib:nibThree forCellReuseIdentifier:kSectionThreeCell];
    
    UINib *nibForth = [UINib nibWithNibName:NSStringFromClass([SectionForthCell class]) bundle:nil];
    [self.tableView registerNib:nibForth forCellReuseIdentifier:kSectionForthCell];
    
    UINib *separatorNib = [UINib nibWithNibName:NSStringFromClass([DiscoverySeparatorCell class]) bundle:nil];
    [self.tableView registerNib:separatorNib forCellReuseIdentifier:kSectionSeparatorCell];
    
    // 初始化Section模型
    [self initGroupOne];
    [self initGroupTwo];
    [self initGroupThree];
    [self initGroupForth];
}

- (void)initGroupOne{
    // 创建模型
    RowItem *item = [RowItem rowItemWihtLeftImage:[UIImage imageNamed:@"icon_explore_follow"] leftTitle:@"关注动态" rightImage:[UIImage imageNamed:@"icon_explore_club"] rightTitle:@"我的Club"];
    // 添加模型数组
    SectionItem *sectionItem = [[SectionItem alloc] init];
    sectionItem.rowItems = [NSMutableArray arrayWithArray:@[item]];
    sectionItem.titleForFooter = @"内容推荐";
    sectionItem.type = BTSectionTypeOne;
    
    [self.sectionGroups addObject:sectionItem];
}

- (void)initGroupTwo {
    // 创建模型
    RowItem *exploreItem = [RowItem rowItemWihtImage:[UIImage imageNamed:@"icon_explore_picture"] cellTitle:@"热门图片"];
    RowItem *articleItem = [RowItem rowItemWihtImage:[UIImage imageNamed:@"icon_explore_article"] cellTitle:@"文章"];
    RowItem *goodslItem = [RowItem rowItemWihtImage:[UIImage imageNamed:@"icon_explore_goods"] cellTitle:@"好物"];

    SectionItem *sectionItem = [[SectionItem alloc] init];
    sectionItem.rowItems = [NSMutableArray arrayWithArray:@[exploreItem, articleItem, goodslItem]];
    sectionItem.titleForFooter = @"图片分类";
    sectionItem.type = BTSectionTypeTwo;
    // 添加模型数组
    [self.sectionGroups addObject:sectionItem];
}

- (void)initGroupThree {    // 从网络上加载广告图片
    UIImageView *imageView = [[UIImageView alloc]init];
    [imageView sd_setImageWithURL:[NSURL URLWithString:@"http://img4q.duitang.com/uploads/people/201605/12/20160512144133_TUt5W.thumb.640_160_c.jpeg"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL)
    {
        // 创建Row 模型
        RowItem *rowItem = [[RowItem alloc] init];
        rowItem.type = BTCellTypeAD;
        rowItem.iconImage = imageView.image;
        
        // 添加到原数组
        SectionItem *sectionItem = self.sectionGroups[1];
        [sectionItem.rowItems addObject:rowItem];
        // 刷新表格
        [self.tableView reloadData];
    }];
}

- (void)initGroupForth {        // 创建表格数据
//    RowItem *rowItem01 = [RowItem rowItemWihtLeftImage:[UIImage imageNamed:@"add_icon_photos"] leftTitle:@"美食菜谱" rightImage:[UIImage imageNamed:@"add_icon_photos"] rightTitle:@"收工DIY"];
    RowItem *rowItem01 = [RowItem rowItemWihtLeftImageURL:[NSURL URLWithString:@"http://img4q.duitang.com/uploads/people/201602/14/20160214154703_scf2F.thumb.72_72_c.jpeg"] leftTitle:@"美食菜谱" rightImageURL:[NSURL URLWithString:@"http://img4q.duitang.com/uploads/people/201602/14/20160214173638_LKuBH.thumb.72_72_c.jpeg"] rightTitle:@"手工DIY"];
    
//    RowItem *rowItem02 = [RowItem rowItemWihtLeftImage:[UIImage imageNamed:@"add_icon_photos"] leftTitle:@"插画绘画" rightImage:[UIImage imageNamed:@"add_icon_photos"] rightTitle:@"家居生活"];
    RowItem *rowItem02 = [RowItem rowItemWihtLeftImageURL:[NSURL URLWithString:@"http://img4q.duitang.com/uploads/people/201602/14/20160214170314_2kRKG.thumb.72_72_c.jpeg"] leftTitle:@"插画绘画" rightImageURL:[NSURL URLWithString:@"http://img4q.duitang.com/uploads/people/201602/02/20160202112337_mPY8J.thumb.72_72_c.jpeg"] rightTitle:@"家居生活"];
    
    RowItem *rowItem03 = [RowItem rowItemWihtLeftImage:[UIImage imageNamed:@"add_icon_photos"] leftTitle:@"旅行" rightImage:nil rightTitle:nil];
    
    RowItem *separatorRowItem = [RowItem rowItemWihtImage:[UIImage imageNamed:@"share_icon_more"] cellTitle:nil];
    separatorRowItem.type = BTCellTypeSeparator;
    
    RowItem *rowItem11 = [RowItem rowItemWihtLeftImage:[UIImage imageNamed:@"add_icon_photos"] leftTitle:@"美食菜谱" rightImage:[UIImage imageNamed:@"add_icon_photos"] rightTitle:@"收工DIY"];
    RowItem *rowItem12 = [RowItem rowItemWihtLeftImage:[UIImage imageNamed:@"add_icon_photos"] leftTitle:@"插画绘画" rightImage:[UIImage imageNamed:@"add_icon_photos"] rightTitle:@"家居生活"];
    
    RowItem *rowItem21 = [RowItem rowItemWihtLeftImage:[UIImage imageNamed:@"add_icon_photos"] leftTitle:@"美食菜谱" rightImage:[UIImage imageNamed:@"add_icon_photos"] rightTitle:@"收工DIY"];
    RowItem *rowItem22 = [RowItem rowItemWihtLeftImage:[UIImage imageNamed:@"add_icon_photos"] leftTitle:@"插画绘画" rightImage:[UIImage imageNamed:@"add_icon_photos"] rightTitle:@"家居生活"];
    RowItem *rowItem23 = [RowItem rowItemWihtLeftImage:[UIImage imageNamed:@"add_icon_photos"] leftTitle:@"美食菜谱" rightImage:[UIImage imageNamed:@"add_icon_photos"] rightTitle:@"收工DIY"];
    RowItem *rowItem24 = [RowItem rowItemWihtLeftImage:[UIImage imageNamed:@"add_icon_photos"] leftTitle:@"美食菜谱" rightImage:nil rightTitle:nil];
    
    SectionItem *sectionItem = [[SectionItem alloc] init];
    sectionItem.rowItems = [NSMutableArray arrayWithObjects:rowItem01,rowItem02,rowItem03,separatorRowItem, rowItem11, rowItem12, separatorRowItem, rowItem21, rowItem22, rowItem23, rowItem24, nil];
    sectionItem.type = BTSectionTypeForth;
    [self.sectionGroups addObject:sectionItem];
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.sectionGroups.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    SectionItem *sectionItem = self.sectionGroups[section];
    return sectionItem.rowItems.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    // 获取模型数据
    SectionItem *sectionItem = self.sectionGroups[indexPath.section];
    RowItem *rowItem = sectionItem.rowItems[indexPath.row];
    
    // 设置单元格数据
    if (sectionItem.type == BTSectionTypeOne)
    {
        SectionOneCell *cell = [tableView dequeueReusableCellWithIdentifier:kSectionOneCell];
        cell.rowItem = rowItem;
        return cell;
        
    }else if (sectionItem.type == BTSectionTypeTwo)
    {
        SectionTwoCell *cell = [tableView dequeueReusableCellWithIdentifier:kSectionTwoCell];
        
        if (rowItem.type == BTCellTypeAD) {     // 显示广告单元格
            cell = [tableView dequeueReusableCellWithIdentifier:kSectionThreeCell];
            cell.item = rowItem;
        }else
        {
            cell = [tableView dequeueReusableCellWithIdentifier:kSectionTwoCell];
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            cell.item = rowItem;
        }
        return cell;
        
    }else if (sectionItem.type == BTSectionTypeThree)
    {
        SectionThreeCell *cell = [tableView dequeueReusableCellWithIdentifier:kSectionThreeCell];
        cell.item = rowItem;
        return cell;
        
    }else if (sectionItem.type == BTSectionTypeForth)
    {
        SectionForthCell *cell = [tableView dequeueReusableCellWithIdentifier:kSectionForthCell];
        if (rowItem.type == BTCellTypeSeparator) {      // 显示分割类型单元格
            cell = [tableView dequeueReusableCellWithIdentifier:kSectionSeparatorCell];
        }
        cell.item = rowItem;
        return cell;
    }else
    {
        UITableViewCell *cell;
        return cell;
    }

}

// 设置脚部标题
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
    SectionItem *sectionItem = self.sectionGroups[section];
    return sectionItem.titleForFooter;
}

// 设置行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    SectionItem *section = self.sectionGroups[indexPath.section];
    RowItem *rowItem = section.rowItems[indexPath.row];
    if (indexPath.section == 0) {
        return 60;
    }else if (rowItem.type == BTCellTypeAD)
    {
        return 130;
    }else
    {
        return 44;
    }
}

@end
